package com.colin.controller;

import com.colin.pojo.ChanPian;
import com.colin.pojo.ChanPianGongXuTimeTabile;
import com.colin.pojo.JiQi;
import com.colin.pojo.User;
import com.colin.service.ChanPinService;
import com.colin.service.JiQiService;
import com.colin.service.ProductAndManchineRelationSercice;
import com.colin.service.UserService;
import com.colin.util.Time_Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping ("user")
public class UserControllor {
    @Autowired
    UserService userService;
    @Autowired
    JiQiService jiQiService;
    @Autowired
    ChanPinService chanPinService;
    @Autowired
    ProductAndManchineRelationSercice productAndManchineRelationSercice;

    //跳转登录界面
    @RequestMapping ("login")
    public String login () {
        return "login";
    }

    @RequestMapping ("logon")
    public String logon () {
        return "logon";
    }

    // 登录界面处理跳转
    @RequestMapping ("doLogin")
    public String doLogin (User user1, HttpSession session) {
        User user = userService.selectLoginUser(user1);
        if (user == null) {
            return "/login";
        }
        else {
            String currentTime = Time_Util.getCurrentTime();
            session.setAttribute("currentTime", currentTime);
            session.setMaxInactiveInterval(3600 * 24);
            session.setAttribute("user", user);
            List<JiQi> jiQiList = jiQiService.selectAll();
            int countJiQiNumber = jiQiService.count();
            session.setAttribute("jiQiList", jiQiList);
            session.setAttribute("countJiQiNumber", countJiQiNumber);
            int countChanPianNumber = chanPinService.count();
            List<ChanPian> chanPianList = chanPinService.selectALL();
            session.setAttribute("countChanPianNumber", countChanPianNumber);
            session.setAttribute("chanPianList", chanPianList);
            List<ChanPianGongXuTimeTabile> listTable = productAndManchineRelationSercice.selelctAll();
            session.setAttribute("listTable", listTable);
            return "redirect:/web/to";
        }
    }

    @RequestMapping ("doLogon")
    public String doLogon (User user) {
        userService.insertUser(user);
        return "redirect:/user/login";
    }

}
